import { createEditor, Editor, Transforms, Element } from 'slate'
const CustomEditor = {
    isBoldMarkActive(editor) {
      const marks = Editor.marks(editor)
      return marks ? marks.bold === true : false
    },
  
    isCodeBlockActive(editor) {
      const [match] = Editor.nodes(editor, {
        match: n => n.type === 'code',
      })
  
      return !!match
    },
  
    toggleBoldMark(editor) {
      const isActive = CustomEditor.isBoldMarkActive(editor)
      if (isActive) {
        Editor.removeMark(editor, 'bold')
      } else {
        Editor.addMark(editor, 'bold', true)
      }
    },
  
    toggleCodeBlock(editor) {
      const isActive = CustomEditor.isCodeBlockActive(editor)
      Transforms.setNodes(
        editor,
        { type: isActive ? null : 'code' },
        // { match: n => Editor.isBlock(editor, n) }
        { match: n => Element.isElement(n) && Editor.isBlock(editor, n) }
      )
    },
}
export default CustomEditor;